<template>
	<view class="padding bg-white y">
		<view v-for="row,i in list" class="x">
			<view v-for="colum,j in row" style="width: 60rpx;height: 60rpx;" class="solid y " @click="go(i,j)"
				:class="colum.has?'bg-white':'bg-gray'">

				<view v-if="colum.has" style="text-align:center;line-height: 60rpx;">
					<text v-if="colum.number==0&&colum.total>0">
						{{colum.total}}
					</text>
					<image v-if="colum.number==1" style="width: 60rpx;height: 60rpx;" src="../../static/炸弹.svg"></text>
				</view>
			</view>
		</view>
		<view class="cu-btn bg-orange margin-top-lg justify-center " @click="">重新开始</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				hasFind: 0
			}
		},
		onLoad() {
			this.refresh()
		},
		methods: {
			refresh() {
				this.hasFind = 0
				this.list = [];
				for (var i = 0; i < 12; i++) {
					let obj = []
					for (var j = 0; j < 12; j++) {
						obj.push({
							number: 0,
							has: false
						})
					}
					this.list.push(obj)
				}
				for (var i = 0; i < 12; i++) {
					var row = Math.floor(Math.random() * 12);
					var col = Math.floor(Math.random() * 12);
					this.list[row][col].number = 1
				}

				for (var i = 0; i < 12; i++) {
					for (var j = 0; j < 12; j++) {
						if (this.list[i][j].number == 0) {
							this.list[i][j].total = this.cal(i, j);
						}

					}
				}
			},
			cal(i, j) {

				let total = 0;
				if (i - 1 >= 0 && this.list[i - 1][j].number == 1) {
					total = total + 1
				}
				if (i + 1 < 12 && this.list[i + 1][j].number == 1) {
					total = total + 1
				}
				if (j - 1 >= 0 && this.list[i][j - 1].number == 1) {
					total = total + 1
				}
				if (j + 1 < 12 && this.list[i][j + 1].number == 1) {
					total = total + 1
				}

				if ((i - 1 >= 0 && j - 1 >= 0) && this.list[i - 1][j - 1].number == 1) {
					total = total + 1
				}
				if (i + 1 < 12 && j + 1 < 12 && this.list[i + 1][j + 1].number == 1) {
					total = total + 1
				}
				if (j - 1 >= 0 && i + 1 < 12 && this.list[i + 1][j - 1].number == 1) {
					total = total + 1
				}
				if (i - 1 >= 0 && j + 1 < 12 && this.list[i - 1][j + 1].number == 1) {
					total = total + 1
				}
				return total;
			},
			go(i, j) {
				let item = this.list[i][j]
				if (item.has) {
					return
				}
				item.has = true;
				this.hasFind = this.hasFind + 1
				if (this.hasFind == 12 * 12 - 12) {
					uni.showToast({
						title: "成功",
						complete: (res) => {
							uni.showModal({
								content: "游戏成功，是否再来一局",
								success: (res) => {
									if (res.confirm) {
										this.refresh()
									}
								}
							})
						}
					})

					return;
				}
				if (item.number == 1) {
					uni.showToast({
						title: "游戏失败",
						icon: 'fail',
						complete: (res) => {
							uni.showModal({
								content: "游戏失败，是否再来一局",
								success: (res) => {
									if (res.confirm) {
										this.refresh()
									}
								}
							})
						}
					})
					const innerAudioContext = uni.createInnerAudioContext();
					innerAudioContext.autoplay = true;
					innerAudioContext.src =
						'https://downsc.chinaz.net/Files/DownLoad/sound1/202001/12439.wav';
					innerAudioContext.onPlay(() => {
						console.log('开始播放');
					});
					innerAudioContext.onError((res) => {
						console.log(res.errMsg);
						console.log(res.errCode);
					});
					return;
				}
				if (item.total == 0) {
					this.find(i, j)
				}


			},
			find(i, j) {
				if (i - 1 >= 0 && this.list[i - 1][j].number == 0) {
					this.go(i - 1, j)
				}
				if (i + 1 < 12 && this.list[i + 1][j].number == 0) {
					this.go(i + 1, j)
				}
				if (j - 1 >= 0 && this.list[i][j - 1].number == 0) {
					this.go(i, j - 1)
				}
				if (j + 1 < 12 && this.list[i][j + 1].number == 0) {
					this.go(i, j + 1)
				}

				if ((i - 1 >= 0 && j - 1 >= 0) && this.list[i - 1][j - 1].number == 0) {
					this.go(i - 1, j - 1)
				}
				if (i + 1 < 12 && j + 1 < 12 && this.list[i + 1][j + 1].number == 0) {
					this.go(i + 1, j + 1)
				}
				if (j - 1 >= 0 && i + 1 < 12 && this.list[i + 1][j - 1].number == 0) {
					this.go(i + 1, j - 1)
				}
				if (i - 1 >= 0 && j + 1 < 12 && this.list[i - 1][j + 1].number == 0) {
					this.go(i - 1, j + 1)
				}
			}
		}
	}
</script>

<style>

</style>